package com.hns.can.safety.service;

import com.hns.can.common.aop.orgTree.DriverInterfaceAnnotation;
import com.hns.can.common.aop.orgTree.LeftOrgAnnotation;
import com.hns.can.common.aop.sedate.SedateAnnotation;
import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.common.service.BaseInfoValueService;
import com.hns.can.common.vo.CommonParamsVo;
import com.hns.can.master.entity.CommMftAlmEntity;
import com.hns.can.safety.vo.RepairTodayFaultVo;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.SearchConditionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实时故障Service
 * @author:zhangzq
 * @email:
 * @version Revision 2.0.0
 */
@Service
public class RealTimeFailureService extends CommonService {
	
	@Autowired
    private FillCodeDscValue codeDscValue;

    @Autowired
    private BaseInfoValueService baseInfoValueService;

    /**
     * 分页查询实时故障
     * @param vo
     * @return
     * @author:zhangzq
     * @email:
     */
    @LeftOrgAnnotation()
    @Transactional
    @SedateAnnotation()
    @DriverInterfaceAnnotation()
    public Pagination getRealTimeFailure(CommonParamsVo vo) throws Exception{
        Pagination page = this.myBatisPage("realTimeFailure.realTimeFailureCount", "realTimeFailure.getRealTimeFailure", vo,CommMftAlmEntity.class);
        baseInfoValueService.fillOrganLineLicPltNo(page.getData());
        return page;
    }

    /**
     * 查询实时故障用于导出
     * @param vo
     * @return
     * @author:zhangzq
     * @email:
     */
    @LeftOrgAnnotation()
    @Transactional
    @SedateAnnotation()
    @DriverInterfaceAnnotation()
    public List<CommMftAlmEntity> getAllRealTimeFailure(CommonParamsVo vo)throws Exception{
        List list = PubUtil.convertListMapToObject(sqlMapDao.selectList("realTimeFailure.getRealTimeFailure", vo), CommMftAlmEntity.class);
        baseInfoValueService.fillOrganLineLicPltNo(list);
        Map<String,String> map = new HashMap<String,String>();
        map.put("mftClCode", "MFTC0000");
        codeDscValue.fillCodeDscValue(list, map);
        return list;
    }

    @Transactional
    public void manualRepirTodayFault(RepairTodayFaultVo vo)throws Exception {
        //手工修复故障状态
        HashMap<String,String> map=new HashMap<String,String>();
        String[] carIds = vo.getCarIds();
        String[] mftTypeCodes = vo.getMftTypeCodes();
        String[] mftDiscCodes = vo.getMftDiscCodes();
        if(PubUtil.isNotEmpty(carIds)&&PubUtil.isNotEmpty(mftTypeCodes)&&PubUtil.isNotEmpty(mftDiscCodes)){
            for(int i=0;i<carIds.length;i++){
                if(PubUtil.isNotEmpty(carIds[i])&&PubUtil.isNotEmpty(mftTypeCodes[i])&&PubUtil.isNotEmpty(mftDiscCodes[i])){
                    map.put("carId",carIds[i]);
                    map.put("mftTypeCode",mftTypeCodes[i]);
                    map.put("mftDiscCode",mftDiscCodes[i]);
                    sqlMapDao.insert("realTimeFailure.manualRepirTodayFault", map);
                }
            }
        }
    }

    @Transactional
    public void manualFault(SearchConditionVo vo)throws Exception {
        //更新故障状态
        sqlMapDao.update("realTimeFailure.manualRepirFault", vo);
        //重新计算故障数
        sqlMapDao.update("realTimeFailure.updateMftTotalAndResolveCount", vo);
    }
}
